﻿using System;
using System.Diagnostics;
using System.Reflection;
using Rhizome.Internal;

namespace Rhizome.Model
{
    public class IfEquals<TProperty> : PropertyAndValueCriterionBase<TProperty>
    {
        public IfEquals(string property, TProperty value)
            : base(property, value)
        {
        }

        public IfEquals(string property) : base(property)
        {
        }

        public IfEquals()
        {
        }

        protected override bool IsTrue(TProperty value)
        {
            return Equals(value, Value);
        }

    	#region Overrides of Object

    	public override string ToString()
    	{
    		return "IfEquals {0} == {1}".With(Property, Value);
    	}

    	#endregion
    }

    public sealed class IfEquals : IfEquals<object>
    {

		private bool _isValueCached;

    	#region Overrides of IfEquals<object>

    	protected override bool IsTrue(object value)
    	{
			if (_isValueCached == false)
			{
				Value = Value.ChangeType(Binding.Type);
				_isValueCached = true;
			}

    		return Equals(value, Value);
    	}

    	#endregion

    	public IfEquals(string property, object value) : base(property, value)
        {
        }

        public IfEquals(string property) : base(property)
        {
        }

        public IfEquals()
        {
        }
    }
}